using player;
using UnityEngine;
using System.Collections.Generic;

public class Player : Entity
{
    [Header("Player")]
    public float moveSpeed;
    public Transform attackCheckPoint;
    public float attackCheckRadius;

    public PlayerInputSet input { get; private set; }
    public Vector2 moveInput { get; private set; }

    Dictionary<StateCode, State> states;

    // ========================================================================

    protected override void Awake()
    {
        base.Awake();

        input = new PlayerInputSet();
        states = new Dictionary<StateCode, State>();
    }

    protected override void Start()
    {
        foreach (var state in GetComponents<State>())
        {
            state.Init(animator, this);
            states.Add(state.state, state);
        }

        base.Start();
    }

    protected override void OnDrawGizmos()
    {
        base.OnDrawGizmos();

        Gizmos.DrawWireSphere(attackCheckPoint.position, attackCheckRadius);
    }

    private void OnEnable()
    {
        input.Enable();

        input.GamePlay.Move.performed += ctx => moveInput = ctx.ReadValue<Vector2>();
        input.GamePlay.Move.canceled += ctx => moveInput = Vector2.zero;
    }

    private void OnDisable()
    {
        input.Disable();
    }

    // ========================================================================

    public State GetState(StateCode state) => states[state];
    public void ChangeState(StateCode newState) => stateMachine.Transition(GetState(newState));
    public void ChangeState(State newState) => stateMachine.Transition(newState);
    public bool TryChangeState(StateCode newState) => stateMachine.TryTransition(GetState(newState));
    public void TryChangeState(State newState) => stateMachine.TryTransition(newState);
}
